/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.util;

import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;
import com.jspx.utils.ArrayUtil;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: chenYuan
 * Date: 11-6-11
 * Time: 下午9:25
 */
public class Hessian {
    private Hessian() {

    }

    static public byte[] toByteArray(Object object) throws IOException {
        if (object==null) return null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        HessianOutput oos = new HessianOutput(bos);
        oos.writeObject(object);
        try {
            return bos.toByteArray();
        } finally {
            if (bos != null)
                bos.close();
            if (oos != null)
                oos.close();
        }
    }

    static public Object readObject(byte[] bytes) throws IOException {
        if (ArrayUtil.isEmpty(bytes)) return null;
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        HessianInput hInput = new HessianInput(bis);
        try {
            return hInput.readObject();
        } finally {
            hInput.close();
        }
    }


}